package com.chengxiaoxiao.web.controller;

import com.chengxiaoxiao.commons.Page;
import com.chengxiaoxiao.constant.Constants;
import com.chengxiaoxiao.domain.Borrow;
import com.chengxiaoxiao.domain.Users;
import com.chengxiaoxiao.service.BorrowService;
import com.chengxiaoxiao.service.ReturnsService;
import com.chengxiaoxiao.service.impl.BorrowServiceImpl;
import com.chengxiaoxiao.service.impl.ReturnsServiceImpl;
import com.chengxiaoxiao.utils.JPushUtils;
import com.chengxiaoxiao.utils.QrCodeCreateUtil;
import com.google.zxing.WriterException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

//@WebServlet(name = "CircleServlet")
public class CircleServlet extends HttpServlet {

    private ReturnsService returnsService = new ReturnsServiceImpl();
    private BorrowService borrowService = new BorrowServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if ("listborrow".equalsIgnoreCase(op)) {
            listBorrow(request, response);
        } else if ("showborrow".equalsIgnoreCase(op)) {
            showBorrow(request, response);
        } else if ("listnoReturn".equalsIgnoreCase(op)) {
            listNoReturn(request, response);
        } else if ("showNoReturn".equalsIgnoreCase(op)) {
            showNoReturn(request, response);
        } else if ("rankborrow".equalsIgnoreCase(op)) {
            rankBorrow(request, response);
        } else if ("listdestory".equalsIgnoreCase(op)) {
            listDestory(request, response);
        } else if ("showBorrowQr".equalsIgnoreCase(op)) {
            showBorrowQr(request, response);
        } else if ("showReturnQr".equalsIgnoreCase(op)) {
            showReturnQr(request, response);
        } else if ("push".equalsIgnoreCase(op)) {
            String content = request.getParameter("content");
            JPushUtils.push(content);

            HashMap<String, String> map = new HashMap<>();

            map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
            map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "推送成功");
        }
    }

    /**
     * 显示归还二维码
     *
     * @param request
     * @param response
     */
    private void showReturnQr(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("image/jpeg");
        //时间戳
        Date date= new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate =formatter.format(date);

        //获取登录管理员
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute(Constants.SESSION_USER_MODEL);
        if(user!=null)
        {

            String json ="{'op':'return','date':'"+currentDate+"','adminId':'"+user.getId()+"'}";
            try {
                QrCodeCreateUtil.createQrCode(response.getOutputStream(), json, 1000, "JPEG");
            } catch (WriterException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 显示借阅二维码
     *
     * @param request
     * @param response
     */
    private void showBorrowQr(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("image/jpeg");

        //时间戳
        Date date= new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate =formatter.format(date);

        //获取登录管理员
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute(Constants.SESSION_USER_MODEL);
        if(user!=null)
        {

            String json ="{'op':'borrow','date':'"+currentDate+"','adminId':'"+user.getId()+"'}";
            try {
                QrCodeCreateUtil.createQrCode(response.getOutputStream(), json, 1000, "JPEG");
            } catch (WriterException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }

    /**
     * 显示损坏书籍列表
     *
     * @param request
     * @param response
     */
    private void listDestory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String num = request.getParameter("num");

        Page page = borrowService.getListPageDestory(num);
        page.setUrl(request.getContextPath() + "/manager/CircleServlet?op=listDestory");

        request.setAttribute("page", page);
        request.getRequestDispatcher("/circle/listdestory.jsp").forward(request, response);
    }

    /**
     * 借阅排行
     *
     * @param request
     * @param response
     */
    private void rankBorrow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String num = request.getParameter("num");
        Page page = borrowService.getListPageBorrowRank(num);
        page.setUrl(request.getContextPath() + "/manager/CircleServlet?op=rankBorrow");

        request.setAttribute("page", page);
        request.getRequestDispatcher("/circle/rankBorrow.jsp").forward(request, response);


    }

    /**
     * 展示未归还书籍的详细信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void showNoReturn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);

        Borrow borrow = borrowService.getbyId(ids);

        request.setAttribute("borrow", borrow);
        request.getRequestDispatcher("/circle/shownoreturn.jsp").forward(request, response);
    }

    /**
     * 显示未归还书籍列表
     *
     * @param request
     * @param response
     */
    private void listNoReturn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String num = request.getParameter("num");
        Page page = borrowService.getListPageByNoReturn(num);
        page.setUrl(request.getContextPath() + "/manager/CircleServlet?op=listNoReturn");

        request.setAttribute("page", page);
        request.getRequestDispatcher("/circle/listnoreturn.jsp").forward(request, response);
    }

    /**
     * 展示借阅详细信息
     *
     * @param request
     * @param response
     */
    private void showBorrow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);

        Borrow borrow = borrowService.getbyId(ids);

        request.setAttribute("borrow", borrow);
        request.getRequestDispatcher("/circle/showborrow.jsp").forward(request, response);
    }

    /**
     * 显示流通中的书籍
     *
     * @param request
     * @param response
     */
    private void listBorrow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String num = request.getParameter("num");

        Page list = borrowService.getListPageBorrow(num);
        list.setUrl(request.getContextPath() + "/manager/CircleServlet?op=listBorrow");

        request.setAttribute("page", list);
        request.getRequestDispatcher("/circle/listborrow.jsp").forward(request, response);
    }
}
